#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : extract_audio_worker.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/4/22 09:53
"""
import shutil
import os
import traceback
from PyQt5.QtCore import pyqtSignal, QThread
from core.audio.extract_audio import batch_extract_audio_from_video, merge_mp3_files


class ExtractAudioWorker(QThread):
    """提取音频线程"""
    failed = pyqtSignal(Exception)

    def __init__(self, input_files, output_folder, status, parent=None):
        super(ExtractAudioWorker, self).__init__()
        self.input_files = input_files
        self.output_folder = output_folder
        self.status = int(status)


    def run(self):
        try:
            files_path = eval(self.input_files)
            r = batch_extract_audio_from_video(files_path, self.output_folder)
            if self.status != 0:
                # 合并mp3文件
                out_file = os.path.normpath(os.path.join(self.output_folder, "merged.mp3"))
                merge_mp3_files(r, out_file)
                for f in r:
                    shutil.rmtree(f)
        except Exception as e:
            self.failed.emit(e)
            traceback.print_exc()
